<?php
/**
 * 系统管理模块
 * @copyright reginx.com
 * $Id: system.mod.php 131 2013-01-05 09:32:54Z reginx.com@gmail.com $
 */
class system_mod extends admin_mod {

    /**
     * 系统信息
     *
     */
    public function infoAct(){
        $this->assign('atitle' , '系统信息');
        $this->display('system/info.html');
    }


    /**
     * iframe上传方式
     *
     */
    function uploadAct(){
        $this->nocache();
        $cb = $this->get('cb');
        $tw = intval($this->get('tw'));
        $th = intval($this->get('th'));
        if(!empty($_FILES)){
            $up  = new upload_lib('jpg|png|gif|jpeg|zip|rar',NUll,'8m');
            $tmp = $up->getFile();
            if($tmp){
                $tmp = array_shift($tmp);
                if($tmp['error'] === 0 || $tmp['error'] == '0'){
                    if($tmp['isimage']){
                        $tw = intval($this->get('tw' , 'P'));
                        $th = intval($this->get('th' , 'P'));
                        $tw = $tw ? $tw : $this->conf['thumb_width'];
                        $th = $th ? $th : $this->conf['thumb_height'];
                        image_lib::thumb($tmp['savePath'].$tmp['saveName'] , 'auto' , $tw , $th);
                        if($this->has('water' , 'P')){
                            image_lib::water($tmp['savePath'].$tmp['saveName'] , $this->conf['data_path'] . 'images/water.png','auto',3);
                        }
                    }
                    $this->assign('error','<span style="color:blue">操作成功!</span>');
                    $this->assign('ret',json_encode(array('url'=>$tmp['saveName'])));
                }else{
                    $this->assign('error',$tmp['error']);
                }
            }else{
                $this->assign('error',$tmp['error']);
            }
        }
        $this->assign('tw' , $tw);
        $this->assign('th' , $th);
        $this->assign('water' , (int)$this->get('water'));
        $this->assign('cb',$cb);
        $this->display('inc/upload.html');
    }

    /**
     * 管理帐号列表
     *
     */
    public function adminAct(){
        $this->assign('atitle' , '帐号列表');
        $this->assign('list' , core::getobj('admin_tab')->order('id asc')->getall());
        $this->display('system/admin.html');
    }

    /**
     * 系统配置页
     *
     */
    public function confAct(){
        if($this->has('misc' , 'P')){
            $data = $this->get('misc' , 'P');
            array_walk($data , array('filter' , 'normal'));
            $obj = core::getobj('misc_tab');
            $obj->set('c' , 0)->set('k' , 'conf')->set('v' , base64_encode(serialize($data)))->replace();
            $this->showmsg('操作成功' , null , 1 , 1);
        }
        $this->assign('misc' , misc_lib::get('conf' , 0));
        $this->assign('atitle' , '站点配置');
        $this->display('system/conf.html');
    }

    /**
     * 管理帐号删除
     *
     */
    public function delmgrAct(){
        $id = intval($this->get('id'));
        if($id === 1){
            $this->showmsg('默认管理帐号无法被删除!' , null , 1 , 3);
        }else{
            core::getobj('admin_tab')->where("id = {$id}")->del();
            $this->showmsg('操作成功' , null , 1 , 1);
        }
    }

    /**
     * 管理帐号修改
     *
     */
    public function addmgrAct(){
        if($this->has('admin' , 'P')){
            $obj = core::getobj('admin_tab');
            if($obj->load('admin')){
                $obj->set('passwd' , md5($obj->data['passwd']));
                $ret = $obj->save();
                if((int)$ret['code'] === 0){
                    $this->showmsg('操作成功' , core::url('admin@system-admin') , 1 , 1);
                }else{
                    $this->showmsg($ret['msg'] , null , 1 , 1);
                }
            }else{
                $this->showmsg($obj->geterr() , null , 1 , 1);
            }
        }else{
            $id = intval($this->get('id'));
            $this->assign('atitle' , '帐号修改');
            $this->assign('admin' , core::getobj('admin_tab')->where('id = ' . $id)->get());
            $this->display('system/addmgr.html');
        }
    }

    /**
     * 缓存管理
     *
     */
    public function cacheAct(){
        if($this->has('ctype' , 'P')){
            $list = $this->get('ctype' , 'P');
            $list = array_map('intval' , $list);
            foreach ($list as $v){
                if($v === 1){
                    $GLOBALS['_CACHE']->clear();
                }
                if($v === 2){
                    core::cleartpl();
                }
            }
            $this->showmsg('操作成功' , null , 1 , 1);
        }
        $this->assign('atitle' , '更新缓存');
        $this->display('system/cache.html');
    }


    /**
     * 中文转换拼音 Ajax 接口
     *
     */
    public function getpyAct(){
        $this->nocache();
        $this->ajaxout(py_lib::get($this->get('cn' , 'P')) , 'text');
    }
}
?>